package core;

import database.ClassDb;
import utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by jimmyhsu on 2016/12/14.
 */
public class RemoveUserInClass extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = StringUtils.getUtf8String(req.getParameter("userId"));
        String classIdStr = StringUtils.getUtf8String(req.getParameter("classId"));
        PrintWriter writer = resp.getWriter();
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(classIdStr)) {
            writer.print("<script>alert('参数错误！')</script>");
            return;
        }
        int classId = Integer.parseInt(classIdStr);
        if (!ClassDb.isUserInClass(userId, classId)) {
            writer.print("<script>alert('该学生已经不在该班中！');history.go(-1);</script>");
        } else {
            ClassDb.deleteUserInClass(userId, classId);
        }
        writer.print("<script>location.href='importClassRight.jsp?classId=" + classId + "&refresh=true';</script>");
    }
}
